package com.jd.finance.core.controller.admin;


import com.jd.finance.core.service.NoticeService;
import com.jd.finance.core.service.OpreateLogService;
import com.jd.finance.core.service.impl.AdminUtilsServiceImpl;
import com.jd.finance.result.BaseResult;
import com.jd.finance.util.IpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 公告表 前端控制器
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-19
 */
@Api(tags = "公告中心")
@Slf4j
@RestController
@RequestMapping("/admin/core/noticeCenter")
public class AdminNoticeController {

    @Resource
    private AdminUtilsServiceImpl adminUtilsServiceimpl;

    @Resource
    private NoticeService noticeService;

    @Autowired
    private OpreateLogService opreateLogService;

    @ApiOperation("查看所有公告")
    @GetMapping("/getAPPAllNotice/{page}/{limit}/{uid}")
    public BaseResult getAPPAllNotice(

            @ApiParam(value = "当前页码",required = true)
            @PathVariable Long page,

            @ApiParam(value = "每页记录数",required = true)
            @PathVariable Long limit,

            @ApiParam(value = "查询操作的uid",required = true)
            @PathVariable Integer uid) {
        if (uid==null) {
            log.error("uid为空! c");
            return BaseResult.error().message("uid为空 c");
        }

        if (!adminUtilsServiceimpl.isInformation(uid)) {
            log.error("当前登录用户不是信息管理员账户");
            return BaseResult.error().message("当前登录用户不是信息管理员账户");
        }

        return noticeService.getAPPAllNotice(uid,page,limit);
    }

    @ApiOperation("根据公告id查询公告")
    @GetMapping("/getAPPNoticeById")
    public BaseResult getAPPNoticeById(Integer uid,Integer nid){
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isInformation(uid)){
            log.error("当前登录用户不是信息管理员账户");
            return BaseResult.error().message("当前登录用户不是信息管理员账户");
        }
        return noticeService.getAPPNoticeById(uid,nid);
    }

    @ApiOperation("根据公告id删除公告")
    @GetMapping("/deleteAPPNoticeById")
    public BaseResult deleteAPPNoticeById(Integer uid, Integer nid, HttpServletRequest request){
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isInformation(uid)){
            log.error("当前登录用户不是信息管理员账户");
            return BaseResult.error().message("当前登录用户不是信息管理员账户");
        }
        boolean add=opreateLogService.add(uid,"添加","添加申请合作银行", IpUtil.getIpAddr(request));
        if (!add) {
            log.error("添加操作日志失败!");
        }
        return noticeService.deleteAPPNoticeById(uid,nid,request);
    }

    @ApiOperation("添加新公告")
    @PostMapping("/insertAPPNotice")
    public BaseResult insertAPPNotice(Integer uid,String title,String text,HttpServletRequest request){
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isInformation(uid)){
            log.error("当前登录用户不是信息管理员账户");
            return BaseResult.error().message("当前登录用户不是信息管理员账户");
        }
        return noticeService.insertAPPNotice(uid,title,text,request);
    }
}

